package com.myboot.controller;


import com.myboot.entity.Article;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ArticleController {
    private static File articleDir;

    static {
        articleDir = new File("./articles");
        if (!articleDir.exists()) {
            articleDir.mkdirs();
        }
    }

    @RequestMapping("/myweb/writeArticle")
    public void writeArticle(HttpServletRequest request, HttpServletResponse response) {
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");
        // 保存文章
        File articleFile = new File(articleDir, title + ".obj");
        if (title == null || content == null || author == null || articleFile.exists()) {
//            File failFile = new File(staticDir, "/myweb/article_fail.html");
//            response.setContentFile(failFile);
            try {
                response.sendRedirect("/myweb/article_fail.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        try (
                FileOutputStream fos = new FileOutputStream(articleFile);
                ObjectOutputStream oos = new ObjectOutputStream(fos)
        ) {
            Article article = new Article(title, author, content);
            oos.writeObject(article);
//            File file = new File(staticDir, "/myweb/article_success.html");
//            response.setContentFile(file);
            response.sendRedirect("/myweb/article_success.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/myweb/showAllArticle")
    public void showAllArticle(HttpServletRequest request, HttpServletResponse response) {
        List<Article> articleList = new ArrayList<>();
        File[] articles = articleDir.listFiles(f -> f.getName().endsWith(".obj"));
        for (File article : articles) {
            try (
                    FileInputStream fis = new FileInputStream(article);
                    ObjectInputStream ois = new ObjectInputStream(fis);
            ) {
                Article art = (Article) ois.readObject();
                articleList.add(art);
            } catch (ClassNotFoundException | IOException e) {
                e.printStackTrace();
            }
        }
        response.setContentType("text/html;charset=utf-8");
        PrintWriter pw = null;
        try {
            pw = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang=\"cn\">");
        pw.println("<head>");
        pw.println("<meta charset=\"UTF-8\">");
        pw.println("<title>文章列表</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<center>");
        pw.println("<h1>文章列表</h1>");
        pw.println("<table border=\"1px\">");
        pw.println("<tr align=\"center\">");
        pw.println("<td>标题</td>");
        pw.println("<td>作者</td>");
        pw.println("</tr>");
        for (Article article : articleList) {
            pw.println("<tr align=\"center\">");
            pw.println("<td>" + article.getTitle() + "</td>");
            pw.println("<td>" + article.getAuthor() + "</td>");
            pw.println("</tr>");
        }
        pw.println("</table>");
        pw.println("</center>");
        pw.println("</body>");
        pw.println("</html>");
    }
}
